package com.andyage.admin.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.andyage.admin.dao.AdFodderMapper;
import com.andyage.admin.service.IAdfodderService;
import com.andyage.bce.Sample;
import com.andyage.bce.UploadFile;
import com.andyage.util.GetUUID;
import com.andyage.util.JsonContent;
import com.andyage.admin.bean.AdfodderBean;

import net.sf.json.JSONObject;

@Service
public class AdfodderServiceImpl implements IAdfodderService {
	String CONTENT_TYPE="text/html;charset=utf-8";
	@Resource
	private AdFodderMapper adfodderMapper;

	@Override
	public void queryAllAdfodderInfo(AdfodderBean adfdderbean, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<AdfodderBean> list=adfodderMapper.queryAllAdfodderInfo(adfdderbean);
		String json=JsonContent.getJsonArray(list, 0);
		response.setContentType(CONTENT_TYPE);
		response.getWriter().println(json);
	}

	@Override
	public void insertAdfodder(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			AdfodderBean adfodder = new AdfodderBean();
			   String fodderId = GetUUID.get();
               String fodderUrl = request.getParameter("fodderUrl");
               String fodderName = request.getParameter("fodderName");
               String fodderDirectoryId = request.getParameter("fodderDirectoryId");
               String fodderType = request.getParameter("fodderType");
               if(fodderType.equalsIgnoreCase(".jpg")||fodderType.equalsIgnoreCase(".png")||fodderType.equalsIgnoreCase(".gif")||fodderType.equalsIgnoreCase(".jpeg")){
            	   adfodder.setFodderTpye(1);
               }else
               if(fodderType.equalsIgnoreCase(".mp4")){
            	   adfodder.setFodderTpye(2);
               }else{
            	   adfodder.setFodderTpye(3);
               }
            	adfodder.setFodderId(fodderId);
               if(fodderUrl!=null && !"".equals(fodderUrl)){
            	   adfodder.setFodderUrl(fodderUrl);
               }
               if(fodderName!=null && !"".equals(fodderName)){
            	   adfodder.setFodderName(fodderName);
               }
               
               if(fodderDirectoryId!=null && !"".equals(fodderDirectoryId)){
            	   adfodder.setFodderDirectoryId(fodderDirectoryId);
               }
              int flag = adfodderMapper.insertAdfodder(adfodder);
              JSONObject json = new JSONObject();
              if(flag!=0){
            		json.accumulate("result", 1);
        			json.accumulate("tips", "上传成功");
        		} else {
        			json.accumulate("result", 0);
        			json.accumulate("tips", "上传失败");
              }
            response.setContentType(CONTENT_TYPE);
      		response.getWriter().print(json);
      }   

	@Override
	public void updateAdfodder(AdfodderBean adfdderbean, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int flag=adfodderMapper.updateAdfodder(adfdderbean);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "修改成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "修改失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void deleteAdfodder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String fodderId= request.getParameter("fodderId");
		int flag=adfodderMapper.deleteAdfodder(fodderId);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "删除成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "删除失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

}
